﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Scenes.ProcessScene.TopMembers;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.ScrollLayout;
using Spinning.Spinning.Interface;
using Spinning.Spinning.Unitity.Storage;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Scenes.ProcessScene.Items
{
    class ItemListScene : BaseScene, ISaveLoad
    {
        List<Item> itemList = new List<Item>();

        public ItemListScene()
        {
            Size = new Vector2(426, 464);
        }

        public override void OnLoad()
        {
            base.OnLoad();


            //List<Unitity.Constants.ItemTypes> list = Game1.stageManager.GameSrate.ChooseItem;
            //list.Add(Unitity.Constants.ItemTypes.Doll);
            //list.Add(Unitity.Constants.ItemTypes.Sandbag);
            //list.Add(Unitity.Constants.ItemTypes.Copter);
            //list.Add(Unitity.Constants.ItemTypes.Marble);
            makeItems();

            
            ScrollMenu scs = new ScrollMenu();//不用改
            scs.Position = new Vector2(11, 168);
            scs.Color = Color.Transparent;
            scs.OnLoad();
            Attach(scs);

            SaveData state = Game1.stageManager.GameSrate;

            //參考用////
            ScrollLayoutElementItem ses = new ScrollLayoutElementItem();
            ses.OnLoad();
            ses.Enable = true;
            ses.OnChangeItemsEvent += UpdateItemList;
            if (state.BuyedItems.Count==0) ses.Quantity = 0;
            else ses.Quantity = state.BuyedItems[0];
            scs.AddItem(ses);

            ses = new ScrollLayoutElementItem();
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Sandbag;
            ses.Enable = true;
            ses.OnChangeItemsEvent += UpdateItemList;
            if (state.BuyedItems.Count == 0) ses.Quantity = 0;
            else ses.Quantity = state.BuyedItems[1];
            scs.AddItem(ses);

            ses = new ScrollLayoutElementItem();
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Copter;
            ses.Enable = true;
            ses.OnChangeItemsEvent += UpdateItemList;
            if (state.BuyedItems.Count == 0) ses.Quantity = 0;
            else ses.Quantity = state.BuyedItems[2];
            scs.AddItem(ses);

            ses = new ScrollLayoutElementItem();
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Windmill;
            ses.Enable = true;
            ses.OnChangeItemsEvent += UpdateItemList;
            if (state.BuyedItems.Count == 0) ses.Quantity = 0;
            else ses.Quantity = state.BuyedItems[3];
            scs.AddItem(ses);

            ses = new ScrollLayoutElementItem();
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Doll;
            ses.Enable = true;
            ses.OnChangeItemsEvent += UpdateItemList;
            if (state.BuyedItems.Count == 0) ses.Quantity = 0;
            else ses.Quantity = state.BuyedItems[4];
            scs.AddItem(ses);
            /////////

            SortChildren();
        }

        private void makeItems()
        {
            //int i = 0;
            Vector2 basepos = new Vector2(60, 90);//102
            Vector2 bgpos = new Vector2(15, 42);//102
            //foreach (Spinning.Unitity.Constants.ItemTypes type in itemIds)
            //{
            //    Item item = new Item();
            //    item.OnLoad();
            //    item.ItemType = type;
            //    item.Position = basepos;
            //    basepos.X += 102;
            //    item.ZIndex = Constants.ui;
            //    Attach(item);
            //}

            for (int i = 0; i < 4; i++)
            {
                Sprite bgSquare = new Sprite();
                bgSquare.textureName = "item_store/bg_square";
                bgSquare.OnLoad();
                bgSquare.ZIndex = Constants.uibackground;
                bgSquare.Position = bgpos;
                bgpos.X += 102;
                Attach(bgSquare);

                Item item = new Item();
                item.OnLoad();
                item.ItemType = Constants.ItemTypes.Empty;
                item.Position = basepos;
                basepos.X += 102;
                item.ZIndex = Constants.ui;
                item.OnReleaseEvent += item.RemoreMyself;
                item.OnReleaseEvent += UpdateItemList;
                TouchManager.RegisteTouchArea(item);
                Attach(item);
                itemList.Add(item);
            }

            UpdateItemList();
        }

        void UpdateItemList(BaseEntity entity, TouchLocation touchLocation) 
        {
            UpdateItemList();
        }

        private void UpdateItemList()
        {
            List<Unitity.Constants.ItemTypes> list = Game1.stageManager.GameSrate.ChooseItem;
            List<Unitity.Constants.ItemTypes> removelist = new List<Constants.ItemTypes>();
            for (int i = 0; i < list.Count; i++) 
            {
                int number = 0;
                switch (list[i]) 
                {
                    case Constants.ItemTypes.Marble:  //0
                        number = 0;
                        break;
                    case Constants.ItemTypes.Sandbag: //1
                        number = 1;
                        break;
                    case Constants.ItemTypes.Copter:  //2
                        number = 2;
                        break;
                    case Constants.ItemTypes.Windmill://3
                        number = 3;
                        break;
                    case Constants.ItemTypes.Doll:    //4
                        number = 4;
                        break;
                }
                if (Game1.stageManager.GameSrate.BuyedItems[number] == 0) removelist.Add(list[i]);
            }

            foreach (Spinning.Unitity.Constants.ItemTypes type in removelist) 
            {
                Game1.stageManager.GameSrate.ChooseItem.Remove(type);
            }

            for (int i = 0; i < itemList.Count; i++)
            {
                itemList[i].ItemType = Constants.ItemTypes.Empty;
            }

            for (int i = 0; i < list.Count; i++)
            {
                itemList[i].ItemType = Game1.stageManager.GameSrate.ChooseItem[i];
            }
        }

        public override BaseScene Clone()
        {
            throw new NotImplementedException();
        }

        public void Save(Unitity.Storage.SaveData state)
        {

        }

        public void Load(Unitity.Storage.SaveData state)
        {

        }
    }
}
